package com.lu.dao.impl;

import com.lu.dao.BaseDao;
import com.lu.dao.TradelogDao;
import com.lu.pojo.Goods;
import com.lu.pojo.Page;
import com.lu.pojo.Tradelog;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class TradelogDaoImpl extends BaseDao implements TradelogDao {
    @Override
    public List<Tradelog> getAllTradelog() {
        List<Tradelog> tradelogList = new ArrayList<>();
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "SELECT TrId,TrName,Price,Seller,Buyer,TradingHour FROM Tradelog";
        try {
            con = getCon();
            pstmt = con.prepareStatement(sql);
            rs = pstmt.executeQuery();
            if (rs != null) {
                while (rs.next()) {
                    int trid = rs.getInt("TrId");
                    String trname = rs.getString("TrName");
                    Double price = rs.getDouble("Price");
                    int seller = rs.getInt("Seller");
                    int buyer = rs.getInt("Buyer");
                    Date tradinghour = rs.getDate("TradingHour");
                    Tradelog tradelog = new Tradelog(trid, trname, price, seller, buyer, tradinghour);
                    tradelogList.add(tradelog);
                }
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            closeAll(con, pstmt, rs);
        }
        return tradelogList;
    }

    @Override
    public List<Tradelog> getSomeTradelogByPage(Page page) {
        List<Tradelog> tradelogList = new ArrayList<>();
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "SELECT TrId,TrName,Price,Seller,Buyer,TradingHour FROM Tradelog limit ?,?";
        try {
            con = getCon();
            pstmt = con.prepareStatement(sql);
            pstmt.setInt(1, (page.getCurrentPage() - 1) * page.getCount());
            pstmt.setInt(2, page.getCount());
            rs = pstmt.executeQuery();
            if (rs != null) {
                while (rs.next()) {
                    int trid = rs.getInt("TrId");
                    String trname = rs.getString("TrName");
                    Double price = rs.getDouble("Price");
                    int seller = rs.getInt("Seller");
                    int buyer = rs.getInt("Buyer");
                    Date tradinghour = rs.getDate("TradingHour");
                    Tradelog tradelog = new Tradelog(trid, trname, price, seller, buyer, tradinghour);
                    tradelogList.add(tradelog);
                }
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            closeAll(con, pstmt, rs);
        }
        return tradelogList;
    }

    @Override
    public int getCountTradelog() {
        int result = 0;
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "SELECT count(1)FROM Tradelog ";
        try {
            con = getCon();
            pstmt = con.prepareStatement(sql);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                result = rs.getInt(1);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            closeAll(con, pstmt, rs);
        }
        return result;
    }

    @Override
    public int addOneTrade(Tradelog tradelog) {
        String sql = "INSERT INTO Tradelog(GId,TrName,Price,Seller,Buyer,TradingHour,Address,CREATED_By,CREATED_Time) VALUES (?,?,?,?,?,NOW(),?,?,NOW())";
        Object[] parma = {tradelog.getGid(), tradelog.getTrname(), tradelog.getPrice(), tradelog.getSeller(), tradelog.getBuyer(), tradelog.getAddress(), tradelog.getCreatedBy()};
        return executeUpdate(sql, parma);
    }
}
